<?php

namespace App\Models\OperationManagement;

use Grimzy\LaravelMysqlSpatial\Types\Point;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;

class SanitationSite extends Model
{
    use SoftDeletes;
    use SpatialTrait;
    protected $guarded = ['sanitation_site_ids'];
    protected $spatialFields = ['location'];

    //查询list
    public function getList($request)
    {
    	$per_page = $request->input('per_page',10);
    	return self::orderBy('id','desc')
                ->with(['organization','category','employees','files'])
                ->where(function($query) use ($request){
                    //检测当前的请求中是否包含name、category_id参数
                    $name = $request->input('name', '');//道路名称
                    $category_id = $request->input('category_id','');//站点类型id
                    $location = $request->input('location','');
                    if(!empty($name)) {
                        $query->where('name','like','%'.$name.'%');
                    }
                    $query->whereNotIn('category_id',[1,2,3]);
                    if(!empty($category_id)) {
                        $query->where('category_id',$category_id);
                    }
                    if(!empty($location)) {
                        $location = Point::fromString($location);
                        $query->distance('location', $location, 10);
                    }
                })
                ->paginate($per_page)->toArray();
    }

    //处理厂、分拣中心list
    public function getOtherList($request)
    {
        $per_page = $request->input('per_page',10);
        $category_id = $request->input('category_id','');//站点类型id
        if($category_id == 1) { //生活垃圾处理厂
            return self::orderBy('id','desc')
                    ->with(['category','employees','files','domestic_treatment_plant'])
                    ->where(function($query) use ($request){
                        //检测当前的请求中是否包含name、category_id参数
                        $name = $request->input('name', '');//名称
                        $category_id = $request->input('category_id','');//站点类型id
                        $location = $request->input('location','');//经纬度
                        $status = $request->input('status','');//状态
                        $query->where('category_id',$category_id);
                        if(!empty($name)) {
                            $query->where('name','like','%'.$name.'%');
                        }
                        if(!empty($location)) {
                            $location = Point::fromString($location);
                            $query->distance('location', $location, 10);
                        }
                        if(!empty($status)) {
                            $query->where('status',$status);
                        }
                    })
                    ->paginate($per_page);
        } else if ($category_id == 2) { //厨余垃圾处理厂
            return self::orderBy('id','desc')
                    ->with(['category','employees','files','kitchen_treatment_plant'])
                    ->where(function($query) use ($request){
                        //检测当前的请求中是否包含name、category_id参数
                        $name = $request->input('name', '');//名称
                        $category_id = $request->input('category_id','');//站点类型id
                        $location = $request->input('location','');//经纬度
                        $status = $request->input('status','');//状态
                        $query->where('category_id',$category_id);
                        if(!empty($name)) {
                            $query->where('name','like','%'.$name.'%');
                        }
                        if(!empty($location)) {
                            $location = Point::fromString($location);
                            $query->distance('location', $location, 10);
                        }
                        if(!empty($status)) {
                            $query->where('status',$status);
                        }
                    })
                    ->paginate($per_page);
        } else if ($category_id == 3) { //二次分拣中心厂
            return self::orderBy('id','desc')
                    ->with(['category','employees','files','transfer_plant'])
                    ->where(function($query) use ($request){
                        //检测当前的请求中是否包含name、category_id参数
                        $name = $request->input('name', '');//名称
                        $category_id = $request->input('category_id','');//站点类型id
                        $location = $request->input('location','');//经纬度
                        $status = $request->input('status','');//状态
                        $query->where('category_id',$category_id);
                        if(!empty($name)) {
                            $query->where('name','like','%'.$name.'%');
                        }
                        if(!empty($location)) {
                            $location = Point::fromString($location);
                            $query->distance('location', $location, 10);
                        }
                        if(!empty($status)) {
                            $query->where('status',$status);
                        }
                    })
                    ->paginate($per_page);
        }
    }



    //查看负责人
    public function employees()
    {
        return $this->belongsTo(\App\Models\SystemManagement\Employee::class,'principal_id','id')->select('id','name');
    }

    //查看组织
    public function organization()
    {
        return $this->belongsTo(\App\Models\SystemManagement\Organization::class,'organization_id','id')->select('id','name');
    }

    //站点类型
    public function category()
    {
        return $this->belongsTo(\App\Models\OperationManagement\SanitationCategory::class,'category_id','id')->select('id','name');
    }

    //生活垃圾处理厂
    public function domestic_treatment_plant()
    {
        return $this->belongsTo(\App\Models\OperationManagement\DomesticTreatmentPlant::class,'rfid','id');
    }

    //厨余垃圾处理厂
    public function kitchen_treatment_plant()
    {
        return $this->belongsTo(\App\Models\OperationManagement\KitchenTreatmentPlant::class,'rfid','id');
    }

    //二次分拣中心厂
    public function transfer_plant()
    {
        return $this->belongsTo(\App\Models\OperationManagement\TransferPlant::class,'rfid','id');
    }

    function files()
    {
        return $this->hasMany(SanitationSiteFile::class,'sanitation_site_id')->with('file');
    }
}
